<?php
class Business_Category
{
    protected $_tableName = "product_category";
    
    public function setType($type)
    {
        $this->_tableName = $type . "_category";
        return $this;
    }
    
    public function getAll()
    {
        $fields = array("id", "name");
        $select = Db_Factory::getInstance()->createSelect();
        $select->from($this->_tableName, $fields);
        $stmt = $select->query();
        $result = $stmt->fetchAll();
        return $result;
    }
    
    /**
     * 添加分类
     * @param String $cateName
     * @return Ambigous <mixed, multitype:>
     */
    public function add($cateName)
    {
    	$table = Db_Factory::getInstance()->createTable($this->_tableName);
    	$data = array(
    		'ctime' => date("Y-m-d H:i:s"),
    		'mtime' => date("Y-m-d H:i:s"),
    		'name' => $cateName
    	);
    	return $table->insert($data);
    }
    
    public function getById($cateId)
    {
    	$fields = array("id", "name");
    	$select = Db_Factory::getInstance()->createSelect();
    	$select->from($this->_tableName, $fields)
    			->where('id = ?', $cateId);
    	$stmt = $select->query();
    	$result = $stmt->fetch();
    	return $result;
    }
    
    public function update($cateName, $cateId)
    {
    	$table = Db_Factory::getInstance()->createTable($this->_tableName);
    	$data = array(
    		'mtime' => date("Y-m-d H:i:s"),
    		'name' => $cateName
    	);
    	$where = array("id = ?" => $cateId);
    	return $table->update($data, $where);
    }
    
    public function delete($cateId)
    {
    	$cateId = intval($cateId);
    	$where = array("id = ?" => $cateId);
    	$table = Db_Factory::getInstance()->createTable($this->_tableName);
    	return $table->delete($where);
    }
}